<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>达达微博</title>
    <style>        body {
        font-family: 'Arial', sans-serif;
        background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
        margin: 0;
        padding: 0;
        color: #333;
    }
    .container {
        width: 80%;
        max-width: 1200px;
        margin: 0 auto;
        padding: 20px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        background-color: #fff;
        border-radius: 10px;
        overflow: hidden;
    }
    h1 {
        text-align: center;
        color: #333;
        margin-bottom: 20px;
        font-size: 2.5em;
        text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
    }
    .user-section {
        text-align: center;
        margin-bottom: 20px;
    }
    .user-section h3 {
        color: #555;
        display: inline-block;
        margin-right: 10px;
        font-size: 1.2em;
    }
    .user-section a {
        margin: 0 10px;
        text-decoration: none;
        color: #007bff;
        transition: color 0.3s, transform 0.3s;
    }
    .user-section a:hover {
        color: #0056b3;
        transform: scale(1.05);
    }
    .user-section span {
        color: darkred;
        font-weight: bold;
    }
    .weibo-list {
        list-style-type: none;
        padding: 0;
    }
    .weibo-item {
        background-color: #f9f9f9;
        border: 1px solid #ddd;
        border-radius: 5px;
        margin-bottom: 15px;
        padding: 15px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        transition: transform 0.2s, box-shadow 0.2s;
    }
    .weibo-item:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    }
    .weibo-item h3 {
        margin: 0 0 10px;
        font-size: 1.1em;
    }
    .weibo-item a {
        text-decoration: none;
        color: darkred;
        transition: color 0.3s, transform 0.3s;
    }
    .weibo-item a:hover {
        color: #c83301;
        transform: scale(1.05);
    }
    hr {
        border: none;
        height: 1px;
        background-color: #ddd;
        margin: 20px 0;
    }
    @media (max-width: 768px) {
        .container {
            width: 95%;
            padding: 15px;
        }
        h1 {
            font-size: 2em;
        }
        .user-section h3 {
            font-size: 1em;
        }
    }
    </style>
</head>
<body>

<div class="container">
    <h1>微博首页</h1>

    <!-- 判断是否登录，显示不同的内容 -->
    <div v-if="isLogin" class="user-section">
        <!-- 把得到的用户昵称显示出来 -->
        <h3>欢迎<span>{{user.nickname}}</span>回来！</h3>
        <a href="/insert.html">发微博</a>
        <!-- href是废掉跳转功能,然后添加点击事件 -->
        <a href="javascript:void(0)" @click="logout()">退出登录</a>
    </div>
    <div v-else class="user-section">
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>

    <hr>

    <!-- 需要有一个数组，数组中存放的是微博对象 -->
    <ul class="weibo-list">
        <li v-for="w in arr" class="weibo-item">
            <h3>
                <a :href="'/detail.html?id='+w.id">
                    <span>{{w.nickname}}</span>: {{w.content}}
                </a>
            </h3>
        </li>
    </ul>
</div>

<!-- 引入axios和vue的框架文件 -->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>    let v = new Vue({
    el: ".container",
    data: {
        isLogin: false,
        user: {},
        arr: []
    },
    methods: {
        logout() {
            axios.get("/v1/users/logout").then(function (response) {
                v.isLogin = false;
            })
        }
    },
    created: function () {
        // 进入到页面就要发请求，获取当前登录的用户对象
        axios.get("/v1/users/currentUser").then(function (response) {
            // 如果登录过得到的response.data是一个UserVO对象，未登录得到的是一个空字符串""
            // 此处给user属性赋值为UserVO
            v.user = response.data;
            if (response.data == "") { // 没有拿到当前登录的对象
                v.isLogin = false;
            } else { // 得到了当前登录的对象
                v.isLogin = true;
            }
        })

        // 发请求获取所有微博数据
        axios.get("/v1/weibo/selectIndex").then(function (response) {
            v.arr = response.data;
        })
    }
})
</script>

</body>
</html>